<template>
  <div>
    <Home_head />
    <div class="personalCenter">
      <div class="user_info">
        <el-upload
          class="avatar-uploader"
          action=""
          :http-request="uploadImge"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :headers="MyHeader"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar1" />
        </el-upload>
        <span class="user_name">{{ userName }}</span>
        <span type="text" class="user_note"  id="user_note">{{userNote}}</span>
      </div>
      <div class="navigationBar">
        <span class="bar" @click="topersonalCenter()">文章</span>
        <span class="bar" @click="toCollectView()">收藏</span>
        <span class="bar" @click="toAttentionView()">关注</span>
        <span class="bar" @click="toSetView()">设置</span>
        <span style="position: absolute; right: 30px;">关注：{{ attentionNum }}</span>
        <span style="position: absolute; right: 120px;">粉丝: {{ fansNum }}</span>
      </div>
      <h1>我的收藏</h1> <br>
      <Collect_card/>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import Home_head from "@/components/Home_head.vue";
import  Collect_card from '@/components/Collect_card.vue'
var token = localStorage.getItem("user_Token");
export default {
  components: {
    Home_head,
    Collect_card,
  },
  data(){
    return{
      imageUrl:
        "http://localhost:5001/note/userimg/007Gzox8ly1hbh8xg1h73j30f40mk0ua-20230314034306397.jpg",
      MyHeader: { Authorization: token },
      userName: "罗添煦",
      userNote: "这个人什么都没有留下",
      attentionNum:200,
      fansNum:100,
  }
  },
  created(){
      this.queryInfo()
  },
  methods:{
    uploadImge(param){
      const formData = new FormData()
      formData.append('file', param.file)
      formData.append("token",token)
      const url = '/uploadImge'
      axios.post(url,formData).then(data => {
        console.log('上传图片成功')
        this.$message({
                message: '头像切换成功',
                type: 'success'
                });
        location.reload();
      }).catch(response => {
       console.log('图片上传失败')
     })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    queryInfo(){
      axios.post("/queryInfo", {
                userToken : localStorage.getItem("user_Token"),
            }).then(response=>{
              this.userName=response.data.userName
              this.imageUrl=response.data.userHead
              if(response.data.userNote!=""){
                if(response.data.userNote.length<10){
                 this.userNote=response.data.userNote
                 this.userAllNote=response.data.userNote
                }else{
                  this.userNote=response.data.userNote.substring(0,9)+"......"
                  this.userAllNote=response.data.userNote
                }
              }else{
                this.userNote="这个人什么都没有留下"
              }
              this.fansNum=response.data.fansNum
              this.attentionNum=response.data.attentionNum
            })
    },
    topersonalCenter(){
        this.$router.push("/personalCenter")
    },
    toCollectView(){
      this.$message({
                message: '请不用重复点击同一个url!',
                type: 'error'
                });
    },
    toAttentionView(){
        this.$router.push("/attention")
      },
      toSetView(){
        this.$router.push("/Set")
      },
  },
}
</script>

<style>
.personalCenter {
  position: absolute;
  top: 50px;
  left: 350px;
  background-color: #fff;
  width: 900px;
  height: 800px;
}
.navigationBar{
  width: 100%;
  height: 45px;
  display: flex;
  align-items: center;
  background-color: #ffffff;
  border-bottom: #000000 solid 1.5px;
}
.user_info {
  display: block;
  background-image: url(../assets/580cbb24aff1e6005043fb5b9c216.jpg);
  height: 300px;
}
.user_name {
  position: absolute;
  left: 130px;
  top: 155px;
  font-size: 23px;
  color: #000000;
}
.user_note {
  position: absolute;
  left: 130px;
  top: 190px;
  border: 0px;
  background-color: rgba(0,0,0,0);
}
.bar{
    color: #000000;
    margin-left: 30px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
  position: absolute;
  left: 20px;
  top: 150px;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar1 {
  width: 80px;
  height: 80px;
  display: block;
}
</style>